#####################################################################
# Product:      KnowledgeEngine (Plone Edition)
#
# File:         BooleanEditlet.py
#
# Copyright:    Copyright (c) 2004, Faulkner Technologies
#
# Author:       Brian Oliver
#
# Description:  This file defines the BooleanEditlet class, that is
#               used to generate boolean value choosers for property editors.
#               (check boxes, radio buttons and dropdown lists.)
#
# See Also:     PropertyEditlet, PropertyRefleciton, 
#               PropertyDefinition, PropertyAccess 
#####################################################################

#####################################################################
## KnowledgeEngine Library Imports
from Products.KnowledgeEngine.Common import *
from Products.KnowledgeEngine.Core import *
from Products.KnowledgeEngine.Editlet import Editlet
from Products.KnowledgeEngine.PropertyTypes import *
from Products.KnowledgeEngine.PropertyFormats import *
from Products.KnowledgeEngine.PropertyStructures import *

#####################################################################
## Zope Library Imports
# (none)
 
#####################################################################
## Python Library Imports
# (none)

#####################################################################
## BooleanEditlet Class Definition
class BooleanEditlet(Editlet):
    """
        An editlet for editing boolean types
    """
    
    #---------------------------------------------------------------#
    
    def isEditable(self, propertyDefinition, propertyAccess=None, value=None, isForConstructor=false, REQUEST=None, context=None):
        """
            Returns if the specified propertyDefinition can be
            edited by this editlet
        """

        return propertyDefinition.getType() == pt_BOOLEAN and propertyDefinition.getStructure() == ps_ATOMIC and not propertyDefinition.hasEnumeratedValues()
        
    #---------------------------------------------------------------#

    def generateEditorFor(self, propertyDefinition, propertyAccess=None, value=None, isForConstructor=false, REQUEST=None, context=None):
        """
            Returns a HTML editlet for the specified property.
        """

        #determine the display format (checkbox, radio or selection) for the property
        propertyFormat = propertyDefinition.getFormat()
        if propertyFormat == pf_CHECKBOX:
            displayFormat = pf_CHECKBOX
            displayValues = {true:'Yes', false:'No'}
            
        elif propertyFormat[0:len(pf_RADIO)] == pf_RADIO:
            displayFormat = pf_RADIO
            
        elif propertyFormat[0:len(pf_SELECTION)] == pf_SELECTION:
            displayFormat = pf_SELECTION
            
        else:
            displayFormat = pf_CHECKBOX
            displayValues = {true:'Yes', false:'No'}

        #determine the display values (for radio and selection formats)
        if displayFormat == pf_RADIO or displayFormat == pf_SELECTION:
            propertyFormat = propertyFormat[len(displayFormat):]
            
            if propertyFormat == pf_YES_NO:
                displayValues = {true:'Yes', false:'No'}
            elif propertyFormat == pf_ON_OFF:
                displayValues = {true:'On', false:'Off'}
            elif propertyFormat == pf_TRUE_FALSE:
                displayValues = {true:'True', false:'False'}
            elif propertyFormat == pf_SELECTED_UNSELECTED:
                displayValues = {true:'Selected', false:'Not Selected'}
            else:
                displayValues = {true:'Yes', false:'No'}
                                
        #the resulting editlet
        result = ""

        #-----------------
        #is the property read only?        
        if propertyDefinition.isReadPermitted() and not propertyDefinition.isWritePermitted():

            #get the value for the property
            if isForConstructor:
                value = propertyDefinition.getDefaultValue()
            else:
                value = propertyAccess.getPropertyValue(propertyDefinition.getIdentity())
            
            #create the editor
            result = "<table border=1><tr><td>%s</td></tr></table>" % (displayValues[value],)
        
        #-----------------
        #is the property writable?
        identity = propertyDefinition.getIdentity()
        if propertyDefinition.isWritePermitted():
        
            #get the value for the property
            if propertyDefinition.isReadPermitted():
                if isForConstructor:
                    value = propertyDefinition.getDefaultValue()
                else:
                    #get the value for the property
                    value = propertyAccess.getPropertyValue(identity)
            else:
                #not readable
                value = false
                
            #create the editor
            #-------------------
            if displayFormat == pf_CHECKBOX:
                result += "<input type='checkbox' id='ke%s' name='ke%s' value='true'" % (identity,identity)
                if value:
                    result += " checked"
                result += ">" 
            
            elif displayFormat == pf_RADIO:
            
                result += "<input class='noborder' type='radio' id='ke%s' name='ke%s' value='true'" % (identity,identity)
                if value:
                    result += " checked"
                result += "><b>%s</b>" % (displayValues[true], )
                
                result += "<br>"
                
                result += "<input class='noborder' type='radio' id='ke%s' name='ke%s' value='false'" % (identity,identity)
                if not value:
                    result += " checked"
                result += "><b>%s</b>" % (displayValues[false], )
            
            else:  #pf_SELECTION
                result += "<select id='ke%s' name='ke%s'>" % (identity,identity)
            
                result += "<option value='true'"
                if value:
                    result += " selected"
                result += ">%s</option>" % (displayValues[true], )
                
                result += "<option value='false'"
                if not value:
                    result += " selected"
                result += ">%s</option>" % (displayValues[false], )
                                  
                result += "</select>"        
            
        return result
        
    #---------------------------------------------------------------#

    def updatePropertyWith(self, propertyDefinition, propertyAccess, REQUEST=None):
        """
            Attempts to update the specified property on the
            object implementing the PropertyAccess interface with the
            specified HTTP REQUEST.
        """

        #is write permitted on the specified property?
        if propertyDefinition.isWritePermitted():
        
            #is the property format a checkbox? (when checkboxes aren't selected, values aren't submitted)
            if propertyDefinition.getFormat() == pf_CHECKBOX or propertyDefinition.getFormat() == pf_NATIVE:
                value = REQUEST.has_key("ke%s" % (propertyDefinition.getIdentity(),))
            else:
                #get the value from the REQUEST
                value = REQUEST["ke%s" % (propertyDefinition.getIdentity(),)] == 'true'
 
            # Check we have a value if it isMandatory
            propertyDefinition.verifyMandatoryValue(value)
                   
            #set the value for the property
            propertyAccess.setPropertyValue(propertyDefinition.getIdentity(), value)
        
    #---------------------------------------------------------------#

#####################################################################
## Class Initialisation

# Register Editlet Information with the Knowledge Engine
registerEditlet("BooleanEditlet", BooleanEditlet())

#####################################################################
